import Vue from 'vue'
import Mint from 'mint-ui'
import 'mint-ui/lib/style.css'
Vue.use(Mint)

import './lib/mui/css/mui.min.css'

import './lib/mui/css/icons-extra.css'
import './lib/mui/css/app.css'
import './lib/mui/fonts/mui.ttf'

import './lib/iconfont/iconfont.eot'
import './lib/iconfont/iconfont.svg'
import './lib/iconfont/iconfont.ttf'
import './lib/iconfont/iconfont.woff'
import './lib/iconfont/iconfont.woff2'

import VuePreview from 'vue-preview'
Vue.use(VuePreview)

import VueRouter from 'vue-router'
Vue.use(VueRouter)
import router from './router.js'

import VueResource from 'vue-resource';
Vue.use(VueResource);
Vue.http.options.root = 'http://www.liulongbin.top:3005';
Vue.http.options.emulateJSON = true; // 设置提交表单的格式xw

import App from './App.vue'

import Vuex from 'vuex'
Vue.use(Vuex)

import moment from 'moment'
Vue.filter('dateFormat', function (timeString, pattern = 'YYYY-MM-DD HH:mm:ss'){
    return moment(timeString).format(pattern)
})

const store = new Vuex.Store({
    state: {
        car: localStorage.getItem('car') ? JSON.parse(localStorage.getItem('car')) : []
    },
    mutations: {
        addToCar(state, goods){
            let found = false
            goods['selected'] = true
            state.car.some(function(obj, index){
                if (obj.goods_id === goods.goods_id) {
                    found = true
                    state.car[index].goods_add_car_count += goods.goods_add_car_count
                    return true
                }
            })
            if (found === false) {
                state.car.unshift(goods)
            }
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        setToCar(state, goods) {
            state.car.some(function(val, index, arr){
                if (val.goods_id === goods.id) {
                    arr[index].goods_add_car_count = goods.count
                    return true
                }
            })
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        deleteItem(state, goods) {
            state.car.some(function(val, index, arr) {
                if (val.goods_id === goods.id) {
                    arr.splice(index, 1)
                    return true
                }
            })
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        changeSelected(state, goods) {
            state.car.some(function(val, index, arr) {
                if (val.goods_id === goods.id) {
                    arr[index].selected = !val.selected
                    return true
                }
            })
            localStorage.setItem('car', JSON.stringify(state.car))
        }
    },
    getters: {
        getCarNum: state => {
            return state.car.length
        },
        getCar: state => {
            return state.car
        },
        getGoodsCount: state => {
            let result = {}
            state.car.forEach(val => result[val.goods_id] = parseInt(val.goods_add_car_count))
            return result
        },
        getGoodsStockQuantity: state => {
            let result = {}
            state.car.forEach(val => result[val.goods_id] = parseInt(val.goods_stock_quantity))
            return result
        },
        getGoodsSelected: state => {
            let result = {}
            state.car.forEach(val => result[val.goods_id] = val.selected)
            return result
        }
    }
})

let vue = new Vue({
    el: '#app',
    render: c => c(App),
    router,
    store
})